package com.vc.demo.utils;


import org.apache.commons.lang.StringUtils;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by glx on 2016/9/12.
 */
public class TimeUtils {
    public static final Date ZERO_DATE = new Date(0);

    private static SimpleDateFormat FORMAT_yyyyMMddHHmmss() {
        return new SimpleDateFormat("yyyyMMddHHmmss");
    }


    private static SimpleDateFormat FORMAT_yyyyMMddHHmm2() {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm");
    }


    private static SimpleDateFormat FORMAT_yyyyMMddHHmm() {
        return new SimpleDateFormat("yyyyMMddHHmm");
    }


    private static SimpleDateFormat FORMAT_yyyyMMddHH() {
        return new SimpleDateFormat("yyyyMMddHH");
    }


    private static SimpleDateFormat FORMAT_yyyyMMdd() {
        return new SimpleDateFormat("yyyyMMdd");
    }


    private static SimpleDateFormat FORMAT_yyyy_MM_dd() {
        return new SimpleDateFormat("yyyy-MM-dd");
    }


    private static SimpleDateFormat FORMAT_yyyy_MM() {
        return new SimpleDateFormat("yyyy-MM");
    }


    private static SimpleDateFormat FORMAT_yyyyMM() {
        return new SimpleDateFormat("yyyyMM");
    }


    private static SimpleDateFormat FORMAT_yyyy() {
        return new SimpleDateFormat("yyyy");
    }


    private static SimpleDateFormat FORMAT_yyyy年MM月dd日HH时mm分ss秒SSS毫秒() {
        return new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒SSS毫秒");
    }


    private static SimpleDateFormat FORMAT_yyyy年MM月dd日HH时mm分ss秒() {
        return new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");
    }


    private static SimpleDateFormat FORMAT_yyyy年MM月dd日HH时mm分() {
        return new SimpleDateFormat("yyyy年MM月dd日HH时mm分");
    }


    private static SimpleDateFormat FORMAT_yyyy年MM月dd日HH时() {
        return new SimpleDateFormat("yyyy年MM月dd日HH时");
    }


    private static SimpleDateFormat FORMAT_MM月dd日HH时() {
        return new SimpleDateFormat("MM月dd日HH时");
    }


    private static SimpleDateFormat FORMAT_yyyy年MM月dd日() {
        return new SimpleDateFormat("yyyy年MM月dd日");
    }


    private static SimpleDateFormat FORMAT_yyyy年MM月() {
        return new SimpleDateFormat("yyyy年MM月");
    }


    private static SimpleDateFormat FORMAT_yyyy年() {
        return new SimpleDateFormat("yyyy年");
    }


    private static SimpleDateFormat FORMAT_MM月dd日() {
        return new SimpleDateFormat("MM月dd日");
    }


    private static SimpleDateFormat FORMAT_DAY_TIME_SSS() {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
    }


    private static SimpleDateFormat FORMAT_DAY_1() {
        return new SimpleDateFormat("yyyy-MM-dd");
    }


    private static SimpleDateFormat FORMAT_DAY_2() {
        return new SimpleDateFormat("MM/dd/yyyy");
    }


    private static SimpleDateFormat FORMAT_DAY_3() {
        return new SimpleDateFormat("yyyy-M-d");
    }


    private static SimpleDateFormat FORMAT_DAY_4() {
        return new SimpleDateFormat("yyyy-MM-dd+HH:mm:ss");
    }


    private static SimpleDateFormat FORMAT_DAY_5() {
        return new SimpleDateFormat("yyyy-MM-dd HH");
    }
    private static SimpleDateFormat FORMAT_DAY_6() {
        return new SimpleDateFormat("yyyy/MM/dd HH:mm");
    }
    private static SimpleDateFormat FORMAT_DAY_7() {
        return new SimpleDateFormat("yyyy/MM/dd");
    }


    private static SimpleDateFormat FORMAT_DAY_TIME() {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    }


    private static SimpleDateFormat FORMAT_TIME() {
        return new SimpleDateFormat("HH:mm:ss");
    }


    private static SimpleDateFormat FORMAT_HHMM() {
        return new SimpleDateFormat("HH:mm");
    }

    //2022-10-17T22:02:00+08:00
    private static SimpleDateFormat FORMAT_YYYY_MM_DDTHHMMSSZ() {
        return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
    }


    /**
     * 获取日期之前十三个月或者周的开始时间（周一）
     *
     * @param date
     * @param dateUnit
     * @return
     */
    public static List<Date> getPre13DateUnits(Date date, String dateUnit) {
        List<Date> lDate = new ArrayList<>();
        if (StringUtils.equals(dateUnit, "week")) {
            Date monday = TimeUtils.currentWeekBeginning(date);
            lDate.add(monday);
            for (; lDate.size() < 13; ) {
                monday = TimeUtils.preWeekBeginning(monday);
                lDate.add(monday);
            }
        } else if (StringUtils.equals(dateUnit, "mouth")) {
            Date monday = TimeUtils.currentMonthBeginning(date);
            lDate.add(monday);
            for (; lDate.size() < 13; ) {
                monday = TimeUtils.preMonthBeginning(monday);
                lDate.add(monday);
            }
        }

        lDate.sort(new Comparator<Date>() {
            @Override
            public int compare(Date o1, Date o2) {
                return o1.compareTo(o2);
            }
        });
        return lDate;
    }

    /**
     * 获取某段时这里写代码片间内的所有日期
     *
     * @param dBegin
     * @param dEnd
     * @return
     */
    public static List<Date> findDates(Date dBegin, Date dEnd) {
        List<Date> lDate = new ArrayList<>();
        lDate.add(dBegin);
        Calendar calBegin = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间
        calBegin.setTime(dBegin);
        // 测试此日期是否在指定日期之后
        while (dEnd.after(calBegin.getTime())) {
            // 根据日历的规则，为给定的日历字段添加或减去指定的时间量
            calBegin.add(Calendar.DAY_OF_MONTH, 1);
            lDate.add(calBegin.getTime());
        }
        return lDate;
    }

    public static Calendar getCalendar() {
        Calendar calendar = Calendar.getInstance();
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        return calendar;
    }

    public static String todayBeginning(Date date) {
        if (date == null) {
            return null;
        }
        Calendar calendar = getCalendar();
        calendar.setTime(date);

        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        return formatDayTime(calendar.getTime());
    }

    public static Date parseyyyy年MM月dd日(String t) {
        return parse(FORMAT_yyyy年MM月dd日(), "^\\d\\d\\d\\d年\\d\\d月\\d\\d日$", t);
    }

    public static Date parse6(String t){
        if (StringUtils.isEmpty(t)) {
            return null;
        }

      return   parse(FORMAT_DAY_6(), "^\\d\\d/\\d\\d/\\d\\d\\d\\d$", t);
    }
    public static Date parse1(String t) {
        Date date = parse(FORMAT_DAY_6(), "^\\d\\d/\\d\\d/\\d\\d\\d\\d$", t);
        if (date==null){
            return    parse(FORMAT_DAY_7(), "^\\d\\d/\\d\\d/\\d\\d\\d\\d$", t);
        }
          return    parse(FORMAT_DAY_6(), "^\\d\\d/\\d\\d/\\d\\d\\d\\d$", t);
    }
    public static Date parse(String t) {
        if (StringUtils.isEmpty(t)) {
            return null;
        }
        //yyyy-MM-dd HH:mm:ss
        Date ret = parse(FORMAT_DAY_TIME(), "^\\d\\d\\d\\d-\\d\\d-\\d\\d\\s\\d\\d:\\d\\d:\\d\\d$", t);
        if (ret == null) {// yyyy-MM-dd
            ret = parse(FORMAT_DAY_1(), "^\\d\\d\\d\\d-\\d\\d-\\d\\d$", t);
        }

        //yyyy-MM-dd HH:mm
        if (ret == null) {
            ret = parse(FORMAT_yyyyMMddHHmm2(), "^\\d\\d\\d\\d-\\d\\d-\\d\\d\\s\\d\\d:\\d\\d$", t);
        }

        if (ret == null) {// MM/dd/yyyy
            ret = parse(FORMAT_DAY_2(), "^\\d\\d/\\d\\d/\\d\\d\\d\\d$", t);
        }
//        if (ret == null) {// MM/dd/yyyy
//            ret = parse(FORMAT_DAY_6(), "^\\d\\d/\\d\\d/\\d\\d\\d\\d$", t);
//        }

        if (ret == null) {// yyyy-M-d
            ret = parse(FORMAT_DAY_3(), "^\\d\\d\\d\\d-\\d\\d?-\\d\\d?$", t);
        }
        if (ret == null) {//yyyy-MM-dd+HH:mm:ss
            ret = parse(FORMAT_DAY_4(), "^\\d\\d\\d\\d-\\d\\d-\\d\\d\\+\\d\\d:\\d\\d:\\d\\d$", t);
        }
        if (ret == null) {// yyyy-MM-dd HH
            ret = parse(FORMAT_DAY_5(), "^\\d\\d\\d\\d-\\d\\d-\\d\\d\\s\\d\\d$", t);
        }
        if (ret == null) {// yyyyMMddHHmmss
            ret = parse(FORMAT_yyyyMMddHHmmss(), "^\\d\\d\\d\\d\\d\\d\\d\\d\\d\\d\\d\\d\\d\\d$", t);
        }
        if (ret == null) {//yyyyMMddHHmm
            ret = parse(FORMAT_yyyyMMddHHmm(), "^\\d\\d\\d\\d\\d\\d\\d\\d\\d\\d\\d\\d$", t);
        }
        if (ret == null) {//yyyyMMddHH
            ret = parse(FORMAT_yyyyMMddHH(), "^\\d\\d\\d\\d\\d\\d\\d\\d\\d\\d$", t);
        }
        if (ret == null) {//yyyyMMdd
            ret = parse(FORMAT_yyyyMMdd(), "^\\d\\d\\d\\d\\d\\d\\d\\d$", t);
        }
        if (ret == null) {//yyyyMM
            ret = parse(FORMAT_yyyyMM(), "^\\d\\d\\d\\d\\d\\d$", t);
        }

        if (ret == null) {//yyyy年MM月dd日HH时mm分ss秒
            ret = parse(FORMAT_yyyy年MM月dd日HH时mm分ss秒(), "^\\d\\d\\d\\d年\\d\\d月\\d\\d日\\d\\d时\\d\\d分\\d\\d秒$", t);
        }
        if (ret == null) {//yyyy年MM月dd日HH时mm分
            ret = parse(FORMAT_yyyy年MM月dd日HH时mm分(), "^\\d\\d\\d\\d年\\d\\d月\\d\\d日\\d\\d时\\d\\d分$", t);
        }
        if (ret == null) {//yyyy年MM月dd日HH时
            ret = parse(FORMAT_yyyy年MM月dd日HH时(), "^\\d\\d\\d\\d年\\d\\d月\\d\\d日\\d\\d时$", t);
        }
        if (ret == null) {//yyyy年MM月dd日
            ret = parse(FORMAT_yyyy年MM月dd日(), "^\\d\\d\\d\\d年\\d\\d月\\d\\d日$", t);
        }
        if (ret == null) {//yyyy年MM月
            ret = parse(FORMAT_yyyy年MM月(), "^\\d\\d\\d\\d年\\d\\d月$", t);
        }
        if (ret == null) {//yyyy年
            ret = parse(FORMAT_yyyy年(), "^\\d\\d\\d\\d年$", t);
        }
        if (ret == null) {// yyyy-MM-dd HH:mm:ss.SSS
            ret = parse(FORMAT_DAY_TIME_SSS(), "^\\d\\d\\d\\d-\\d\\d-\\d\\d\\s\\d\\d:\\d\\d:\\d\\d\\.\\d\\d\\d$", t);
        }
        if (ret == null) {//yyyy
            ret = parse(FORMAT_yyyy(), "^\\d\\d\\d\\d$", t);
        }

        return ret;
    }





    public static Date parse(SimpleDateFormat simpleDateFormat, String reg, String t) {
//        Pattern pattern = Pattern.compile(reg);
//        if (!pattern.matcher(t).find()) {
//            return null;
//        }


        if (StringUtils.isEmpty(t)) {
            return null;
        }

        try {
            return simpleDateFormat.parse(t);
        } catch (Exception e) {

            System.out.println(e.getLocalizedMessage()+simpleDateFormat.toPattern());
            return null;
        }
    }

    public static String format_yyyyMMddHHmmss(Date date) {
        if (date == null) {
            return null;
        }
        return FORMAT_yyyyMMddHHmmss().format(date);
    }

    public static String format_FORMAT_yyyy年MM月dd日HH时mm分ss秒SSS毫秒(Date date) {
        if (date == null) {
            return null;
        }
        return FORMAT_yyyy年MM月dd日HH时mm分ss秒SSS毫秒().format(date);
    }

    public static String format_FORMAT_yyyy_MM_ddHH_mm(Date date) {
        if (date == null) {
            return null;
        }
        return FORMAT_yyyyMMddHHmm2().format(date);
    }

    public static String format_FORMAT_DAY_TIME_SSS(Date date) {
        if (date == null) {
            return null;
        }
        return FORMAT_DAY_TIME_SSS().format(date);
    }

    public static String format_yyyyMMddHHmm(Date date) {
        if (date == null) {
            return null;
        }
        return FORMAT_yyyyMMddHHmm().format(date);
    }

    public static String format_yyyyMMddHH(Date date) {
        if (date == null) {
            return null;
        }
        return FORMAT_yyyyMMddHH().format(date);
    }

    public static String format_MM月dd日HH时(Date date) {
        if (date == null) {
            return null;
        }
        return FORMAT_MM月dd日HH时().format(date);
    }


    public static String format_yyyy_MM_dd(Date date) {
        if (date == null) {
            return null;
        }
        return FORMAT_yyyy_MM_dd().format(date);
    }

    public static String format_yyyyMMdd(Date date) {
        if (date == null) {
            return null;
        }
        return FORMAT_yyyyMMdd().format(date);
    }


    public static String format_yyyy_MM(Date date) {
        if (date == null) {
            return null;
        }
        return FORMAT_yyyy_MM().format(date);
    }

    public static String format_yyyy(Date date) {
        if (date == null) {
            return null;
        }
        return FORMAT_yyyy().format(date);
    }


    public static String format_yyyy年MM月dd日HH时mm分ss秒(Date date) {
        if (date == null) {
            return null;
        }
        return FORMAT_yyyy年MM月dd日HH时mm分ss秒().format(date);
    }


    public static String format_yyyy年MM月dd日HH时mm分(Date date) {
        if (date == null) {
            return null;
        }
        return FORMAT_yyyy年MM月dd日HH时mm分().format(date);
    }

    public static String format_yyyy年MM月dd日HH时(Date date) {
        if (date == null) {
            return null;
        }
        return FORMAT_yyyy年MM月dd日HH时().format(date);
    }

    public static String format_MM月dd日(Date date) {
        if (date == null) {
            return null;
        }
        return FORMAT_MM月dd日().format(date);
    }

    public static String format_yyyy年MM月dd日(Date date) {
        if (date == null) {
            return null;
        }
        return FORMAT_yyyy年MM月dd日().format(date);
    }

    public static String format_yyyy年MM月(Date date) {
        if (date == null) {
            return null;
        }
        return FORMAT_yyyy年MM月().format(date);
    }

    public static String format_yyyy年(Date date) {
        if (date == null) {
            return null;
        }
        return FORMAT_yyyy年().format(date);
    }


    public static String formatDay(Date date) {
        if (date == null) {
            return null;
        }
        return FORMAT_DAY_1().format(date);
    }

    public static String formatDayTime(Date date) {
        if (date == null) {
            return null;
        }
        return FORMAT_DAY_TIME().format(date);
    }

    public static String formatTime(Date date) {
        if (date == null) {
            return null;
        }
        return FORMAT_TIME().format(date);
    }
    public static String formatDayTime5(Date date) {
        if (date == null) {
            return null;
        }
        return FORMAT_DAY_5().format(date);
    }

    public static String formatHHMM(Date date) {
        if (date == null) {
            return null;
        }
        return FORMAT_HHMM().format(date);
    }

    public static Date nextMinuteBeginning(Date date) {
        if (date == null) {
            return null;
        }
        Calendar calendar = getCalendar();
        calendar.setTime(date);
        calendar.add(Calendar.MINUTE, 1);

        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        return calendar.getTime();
    }

    public static Date preMinuteBeginning(Date date) {
        if (date == null) {
            return null;
        }
        Calendar calendar = getCalendar();
        calendar.setTime(date);
        calendar.add(Calendar.MINUTE, -1);

        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();

    }

    public static Date currentMinuteBeginning(Date date) {
        if (date == null) {
            return null;
        }
        Calendar calendar = getCalendar();
        calendar.setTime(date);
        // calendar.add(Calendar.DATE, -1);

        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();

    }


    public static Date nextHourBeginning(Date date) {
        if (date == null) {
            return null;
        }
        Calendar calendar = getCalendar();
        calendar.setTime(date);
        calendar.add(Calendar.HOUR_OF_DAY, 1);

        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        return calendar.getTime();
    }

    public static Date preHourBeginning(Date date) {
        if (date == null) {
            return null;
        }
        Calendar calendar = getCalendar();
        calendar.setTime(date);
        calendar.add(Calendar.HOUR_OF_DAY, -1);

        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();

    }

    public static Date currentHourBeginning(Date date) {
        if (date == null) {
            return null;
        }
        Calendar calendar = getCalendar();
        calendar.setTime(date);

        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();

    }


    public static Date nextDayBeginning(Date date) {
        if (date == null) {
            return null;
        }
        Calendar calendar = getCalendar();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, 1);

        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        return calendar.getTime();
    }

    public static Date currentDayBeginning(Date date) {
        if (date == null) {
            return null;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        return calendar.getTime();
    }

    public static Date preDayBeginning(Date date) {
        if (date == null) {
            return null;
        }
        Calendar calendar = getCalendar();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, -1);

        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();

    }


    public static Date nextWeekBeginning(Date date) {
        if (date == null) {
            return null;
        }
        Calendar calendar = getCalendar();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, 7);

        calendar.set(Calendar.DAY_OF_WEEK, 2);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }


    public static Date preWeekBeginning(Date date) {
        if (date == null) {
            return null;
        }
        Calendar calendar = getCalendar();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, -7);

        calendar.set(Calendar.DAY_OF_WEEK, 2);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    public static Date currentWeekBeginning(Date date) {
        if (date == null) {
            return null;
        }
        Calendar calendar = getCalendar();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, -7);

        calendar.set(Calendar.DAY_OF_WEEK, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 24);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    public static Date getPreDaysDate(Date date, int n) {
        if (date == null) {
            return null;
        }
        Calendar calendar = getCalendar();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, -n);
//
//        calendar.set(Calendar.DAY_OF_WEEK, 0);
//        calendar.set(Calendar.HOUR_OF_DAY, 0);
//        calendar.set(Calendar.MINUTE, 0);
//        calendar.set(Calendar.SECOND, 0);
//        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }


    public static Date nextMonthBeginning(Date date) {
        if (date == null) {
            return null;
        }
        Calendar calendar = getCalendar();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, 1);

        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }


    public static Date preMonthBeginning(Date date) {
        if (date == null) {
            return null;
        }
        Calendar calendar = getCalendar();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, -1);

        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    public static Date currentMonthBeginning(Date date) {
        if (date == null) {
            return null;
        }
        Calendar calendar = getCalendar();
        calendar.setTime(date);
        // calendar.add(Calendar.MONTH, -1);

        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }


    public static Date nextYearBeginning(Date date) {
        if (date == null) {
            return null;
        }
        Calendar calendar = getCalendar();
        calendar.setTime(date);
        calendar.add(Calendar.YEAR, 1);

        calendar.set(Calendar.DAY_OF_YEAR, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }


    public static Date preYearBeginning(Date date) {
        if (date == null) {
            return null;
        }
        Calendar calendar = getCalendar();
        calendar.setTime(date);
        calendar.add(Calendar.YEAR, -1);

        calendar.set(Calendar.DAY_OF_YEAR, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    public static Date currentYearBeginning(Date date) {
        if (date == null) {
            return null;
        }
        Calendar calendar = getCalendar();
        calendar.setTime(date);
        //calendar.add(Calendar.YEAR, 2);

        calendar.set(Calendar.DAY_OF_YEAR, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    public static Date nextNYear(Date date, int n) {
        if (date == null) {
            return null;
        }
        Calendar calendar = getCalendar();
        calendar.setTime(date);
        calendar.add(Calendar.YEAR, n);

        return calendar.getTime();
    }

    public static Date nextNMonth(Date date, int n) {
        if (date == null) {
            return null;
        }
        Calendar calendar = getCalendar();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, n);

        return calendar.getTime();
    }


    public static Date nextNDay(Date date, int n) {
        if (date == null) {
            return null;
        }
        Calendar calendar = getCalendar();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, n);

        return calendar.getTime();
    }

    public static Date nextNHour(Date date, int n) {
        if (date == null) {
            return null;
        }
        Calendar calendar = getCalendar();
        calendar.setTime(date);
        calendar.add(Calendar.HOUR, n);

        return calendar.getTime();
    }

    public static Date nextNMinute(Date date, int n) {
        if (date == null) {
            return null;
        }
        Calendar calendar = getCalendar();
        calendar.setTime(date);
        calendar.add(Calendar.MINUTE, n);

        return calendar.getTime();
    }


    public static Date nextNSecond(Date date, int n) {
        if (date == null) {
            return null;
        }
        Calendar calendar = getCalendar();
        calendar.setTime(date);
        calendar.add(Calendar.SECOND, n);

        return calendar.getTime();
    }


    public static int getYearsFrom(Date from) {
        Date now = new Date();
        int userYear = now.getYear() - from.getYear();
        return userYear;
    }


    public static int getMonthsFrom(Date from) {
        Date now = new Date();
        int userYear = now.getYear() - from.getYear();
        int userMonth = userYear * 12 + (now.getMonth() - from.getMonth());
        return userMonth;
    }


    public static int getWeeksFrom(Date from) {
        Date now = new Date();

        Date w2 = TimeUtils.currentWeekBeginning(from);
        Date w1 = TimeUtils.currentWeekBeginning(now);

        return (int) ((w1.getTime() + 1 - w2.getTime()) / (1000L * 3600L * 24L * 7L));
    }

    public static int getDaysFrom(Date from) {
        Date now = new Date();

        Date d2 = TimeUtils.currentDayBeginning(from);
        Date d1 = TimeUtils.currentDayBeginning(now);
        return (int) ((d1.getTime() + 1 - d2.getTime()) / (1000L * 3600L * 24L));
    }


    public static int getYearsFrom(Date now, Date from) {
        int userYear = now.getYear() - from.getYear();
        return userYear;
    }


    public static int getMonthsFrom(Date now, Date from) {
        int userYear = now.getYear() - from.getYear();
        int userMonth = userYear * 12 + (now.getMonth() - from.getMonth());
        return userMonth;
    }


    public static int getWeeksFrom(Date now, Date from) {
        Date w2 = TimeUtils.currentWeekBeginning(from);
        Date w1 = TimeUtils.currentWeekBeginning(now);

        return (int) ((w1.getTime() + 1 - w2.getTime()) / (1000L * 3600L * 24L * 7L));
    }

    public static int getDaysFrom(Date now, Date from) {

        Date d2 = TimeUtils.currentDayBeginning(from);
        Date d1 = TimeUtils.currentDayBeginning(now);
        return (int) ((d1.getTime() + 1 - d2.getTime()) / (1000L * 3600L * 24L));
    }


    public static int getDayOfWeek_baseon_MondayIs1_SundayIs7(Date date) {
        int w = getCalendar().get(Calendar.DAY_OF_WEEK);
        switch (w) {
            case Calendar.MONDAY:
                return 1;
            case Calendar.TUESDAY:
                return 2;
            case Calendar.WEDNESDAY:
                return 3;
            case Calendar.THURSDAY:
                return 4;
            case Calendar.FRIDAY:
                return 5;
            case Calendar.SATURDAY:
                return 6;
            case Calendar.SUNDAY:
                return 7;
            default:
                throw new RuntimeException("未知day");
        }
    }


    private static void println(String str, Date date) {
        System.out.println(str + "    " + format_yyyy年MM月dd日HH时mm分ss秒(date));
    }

    public static void main(String[] args) {


        System.out.println(formatDayTime(new Date()));
    }

    /**
     * 获取当前日期是星期几<br>
     *
     * @param date
     * @return 当前日期是星期几
     */
    public static Integer getWeekIndex(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        return w;
    }
}
